package com.ahdy.controller;

import com.ahdy.entity.EmergencyContact;
import com.ahdy.service.EmergencyContactService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 紧急联系人管理控制器
 */
@RestController
@RequestMapping("/api/emergency-contacts")
public class EmergencyContactController {

    @Autowired
    private EmergencyContactService emergencyContactService;

    /**
     * 添加紧急联系人
     * 
     * @param contact 紧急联系人实体
     * @return 响应结果
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> addEmergencyContact(@Valid @RequestBody EmergencyContact contact) {
        Integer id = emergencyContactService.addEmergencyContact(contact);
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "添加紧急联系人成功");
        response.put("data", id);
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    /**
     * 更新紧急联系人
     * 
     * @param id      联系人ID
     * @param contact 紧急联系人实体
     * @return 响应结果
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateEmergencyContact(@PathVariable Integer id,
            @Valid @RequestBody EmergencyContact contact) {
        contact.setId(id);
        int rows = emergencyContactService.updateEmergencyContact(contact);

        Map<String, Object> response = new HashMap<>();
        if (rows > 0) {
            response.put("code", 200);
            response.put("message", "更新紧急联系人成功");
        } else {
            response.put("code", 404);
            response.put("message", "紧急联系人不存在");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 删除紧急联系人
     * 
     * @param id 联系人ID
     * @return 响应结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteEmergencyContact(@PathVariable Integer id) {
        int rows = emergencyContactService.deleteEmergencyContact(id);

        Map<String, Object> response = new HashMap<>();
        if (rows > 0) {
            response.put("code", 200);
            response.put("message", "删除紧急联系人成功");
        } else {
            response.put("code", 404);
            response.put("message", "紧急联系人不存在");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 根据ID查询紧急联系人
     * 
     * @param id 联系人ID
     * @return 响应结果
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getEmergencyContactById(@PathVariable Integer id) {
        EmergencyContact contact = emergencyContactService.getEmergencyContactById(id);

        Map<String, Object> response = new HashMap<>();
        if (contact != null) {
            response.put("code", 200);
            response.put("message", "获取紧急联系人成功");
            response.put("data", contact);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 404);
            response.put("message", "紧急联系人不存在");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 根据老人ID查询紧急联系人列表
     * 
     * @param elderlyId 老人ID
     * @return 响应结果
     */
    @GetMapping("/elderly/{elderlyId}")
    public ResponseEntity<Map<String, Object>> getEmergencyContactsByElderlyId(@PathVariable Integer elderlyId) {
        List<EmergencyContact> contacts = emergencyContactService.getEmergencyContactsByElderlyId(elderlyId);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取老人紧急联系人列表成功");
        response.put("data", contacts);
        return ResponseEntity.ok(response);
    }

    /**
     * 根据老人ID查询主要紧急联系人
     * 
     * @param elderlyId 老人ID
     * @return 响应结果
     */
    @GetMapping("/elderly/{elderlyId}/primary")
    public ResponseEntity<Map<String, Object>> getPrimaryEmergencyContactByElderlyId(@PathVariable Integer elderlyId) {
        EmergencyContact contact = emergencyContactService.getPrimaryEmergencyContactByElderlyId(elderlyId);

        Map<String, Object> response = new HashMap<>();
        if (contact != null) {
            response.put("code", 200);
            response.put("message", "获取主要紧急联系人成功");
            response.put("data", contact);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 404);
            response.put("message", "主要紧急联系人不存在");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 根据老人ID删除所有紧急联系人
     * 
     * @param elderlyId 老人ID
     * @return 响应结果
     */
    @DeleteMapping("/elderly/{elderlyId}")
    public ResponseEntity<Map<String, Object>> deleteEmergencyContactsByElderlyId(@PathVariable Integer elderlyId) {
        int rows = emergencyContactService.deleteEmergencyContactsByElderlyId(elderlyId);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "删除老人所有紧急联系人成功");
        response.put("data", rows);
        return ResponseEntity.ok(response);
    }

    /**
     * 根据老人ID和优先级获取紧急联系人列表
     * 
     * @param elderlyId   老人ID
     * @param maxPriority 最大优先级
     * @return 响应结果
     */
    @GetMapping("/elderly/{elderlyId}/priority/{maxPriority}")
    public ResponseEntity<Map<String, Object>> getEmergencyContactsByElderlyIdAndPriority(
            @PathVariable Integer elderlyId,
            @PathVariable Integer maxPriority) {

        List<EmergencyContact> contacts = emergencyContactService.getEmergencyContactsByElderlyIdAndPriority(elderlyId,
                maxPriority);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取指定优先级紧急联系人列表成功");
        response.put("data", contacts);
        return ResponseEntity.ok(response);
    }
}